﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Core.BOL;
using Core.DAL;
using Core.Utils;

namespace ScoutNET.Controllers
{
    public class AdministrationController : ApplicationController
    {
        private StatMailsRepository sr = new StatMailsRepository();
        private SectionRepository ser = new SectionRepository();
        private UsersRepository ur = new UsersRepository();

        #region Views
        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Statistics()
        {
            tSite_Section section = ser.GetUserSectionByUserAndRole();
            if (section != null)
            {
                // Meta Tag
                ViewData["PageTitle"] = Resources.Administration_Index_Title.Replace("[SECTION]", section.Section.ToLower());
                ViewData["PageDescription"] = Resources.Administration_Index_Description;

                //Breadcrumb
                string[] saBreadcrumb = new String[2];
                saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
                saBreadcrumb[1] = Resources.Administration_Index_Breadcrumb.Replace("[SECTION]", section.Section.ToLower());
                ViewData["Breadcrumb"] = saBreadcrumb;

                List<vw_Mail_MailsView> lMailsViews = sr.SelectMailViewsBySection(section.ID);
                ViewData["MailsViews"] = lMailsViews;

                return View();
            }
            else
            {
                return View("Error404");
            }
        }

        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Email()
        {
            tSite_Section section = ser.GetUserSectionByUserAndRole();
            if (section != null)
            {
                // Meta Tag
                ViewData["PageTitle"] = Resources.Administration_Email_Title.Replace("[SECTION]", section.Section.ToLower());
                ViewData["PageDescription"] = Resources.Administration_Index_Description;

                //Breadcrumb
                string[] saBreadcrumb = new String[2];
                saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
                saBreadcrumb[1] = Resources.Administration_Email_Breadcrumb.Replace("[SECTION]", section.Section.ToLower());
                ViewData["Breadcrumb"] = saBreadcrumb;

                return View();
            }
            else
            {
                return View("Error404");
            }
        }

        [Authorize(Roles = "Animateur,C.U")]
        [HttpPost]
        public ActionResult GetEmail()
        {
            tSite_Section section = ser.GetUserSectionByUserAndRole();
            if (section != null)
            {
                List<GmailItem> lMail = new List<GmailItem>();
                string key = "GMailItems_" + section.SystemName;

                if (this.HttpContext.Cache[key] == null)
                {
                    Core.Utils.GmailClient client = new Core.Utils.GmailClient(section.EmailLogin, section.EmailPassword);
                    
                    List<GmailItem> lMailItems = client.GetUnreadMail();
                    if (lMailItems != null)
                    {
                        HttpContext.Cache.Insert(key, lMailItems, null, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration);
                    }
                }

                lMail = (List<GmailItem>)HttpContext.Cache[key];

                return this.Json(lMail);
            }

            return new EmptyResult();
        }
        #endregion
    }
}
